﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GatewayInspect.Common;
using System.Data.OracleClient;

namespace GatewayInspect.Modules
{
    public partial class BlacklistAddForm : PopupForm
    {
        public BlacklistAddForm()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtTruckNo.Text) && string.IsNullOrWhiteSpace(textDriverID.Text))
            {
                MessageBox.Show("车牌号和司机身份证号不能同时为空！");
                txtTruckNo.Focus();
                return;
            }
            if (CheckBlacklistExit())
            {
                MessageBox.Show("该车辆信息已加入黑名单，不能重复加入！");
                return;
            }
            if (AddToBlacklist())
            {
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBox.Show("添加失败，请与管理员联系！");
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private bool CheckBlacklistExit()
        {
            string strSQL = "select * from T_TRUCK_BLACKLIST where ISDEL = '0'";
            if (!string.IsNullOrWhiteSpace(txtTruckNo.Text))
            {
                strSQL += " and TRUCKNO = '" + txtTruckNo.Text + "'";
            }
            else
            {
                strSQL += " and TRUCKNO is null";
            }
            if (!string.IsNullOrWhiteSpace(textDriverID.Text))
            {
                strSQL += " and DRIVERID = '" + textDriverID.Text + "'";
            }
            else
            {
                strSQL += " and DRIVERID is null";
            }
            DataTable dtBlacklist = DataAccess.ExecuteDataTable(strSQL);
            if (dtBlacklist != null && dtBlacklist.Rows.Count > 0)
            {
                return true;
            }
            return false;
        }

        private bool AddToBlacklist()
        {
            string strSQL = "insert into T_TRUCK_BLACKLIST(ID, TRUCKNO, DRIVERID, DRIVERNAME, REASON, ADDMAN, ADDTIME, ISDEL, ORGCODE, TODATE)"
               + " values(:ID, :TRUCKNO, :DRIVERID, :DRIVERNAME, :REASON, :ADDMAN, :ADDTIME, :ISDEL, :ORGCODE, :TODATE)";
            int i = DataAccess.ExecuteNonQuery(strSQL, new OracleParameter(":ID", Guid.NewGuid().ToString()),
                new OracleParameter(":TRUCKNO", txtTruckNo.Text),
                new OracleParameter(":DRIVERID", textDriverID.Text),
                new OracleParameter(":DRIVERNAME", textDriverName.Text),
                new OracleParameter(":REASON", txtReason.Text),
                new OracleParameter(":ADDMAN", Session.CurrentUserName),
                new OracleParameter(":ADDTIME", DateTime.Now),
                new OracleParameter(":ISDEL", "0"),
                new OracleParameter(":ORGCODE", Session.CurrentOrgCode),
                new OracleParameter(":TODATE", dtpYXQ.Checked ? (object)dtpYXQ.Value.Date : DBNull.Value));
            return i > 0;
        }

        #region 读身份证

        private IDCradReader reader = null;

        protected override void OnLoad()
        {
            base.OnLoad();
            BeginRead();//启动身份证读卡设备开始读卡 
        }

        protected override void OnClosed()
        {
            base.OnClosed();
            if (reader != null)
            {
                reader.AfterReadInfo -= new IDCradReader.AfterReadInfoHandler(reader_AfterReadInfo);
                reader.CheckHandler();//检查是否还有其他对象读卡
            }
        }

        private void BeginRead()
        {
            if (reader == null)
            {
                reader = IDCradReader.GetInstance();
                reader.AfterReadInfo += new IDCradReader.AfterReadInfoHandler(reader_AfterReadInfo);
            }
            reader.BeginRead();
        }

        private void reader_AfterReadInfo(CardInfo info)
        {
            if (IsActive && info != null)
            {
                this.textDriverID.Text = info.CardNo;
                this.textDriverName.Text = info.Name;
            }
        }

        #endregion
    }
}
